package com.zframeworks;

import com.alibaba.fastjson.JSONObject;
import com.zframeworks.core.bean.Message;
import com.zframeworks.core.bean.MessageType;
import com.zframeworks.core.bean.Result;
import com.zframeworks.core.util.JsonUtil;
import com.zframeworks.core.z;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

@Service
public class ImService {

    /**
     * 发送系统通知
     * @param session
     * @param msg
     */
    public void SendSystemMessage(WebSocketSession session,String msg){
        if(z.isNotNull(msg)){
            Message m = new Message();
            m.setZid(z.newNumber());
            m.setType(MessageType.system_message);
            m.setMsg(msg);
            //发送
            SendMessage(session,m);
        }else{
            z.Error("msg内容不可为空");
        }
    }

    /**
     * 发送信息
     * @param session
     * @param m
     * @return
     */
    public void SendMessage(WebSocketSession session,Message m){
        try {
            if(z.isNotNull(session)){
                if(session.isOpen()){
                    if(z.isNotNull(m)){
                        String mjson = JsonUtil.getJson(m);
                        TextMessage tm = new TextMessage(mjson.getBytes());
                        session.sendMessage(tm);
                    }else{
                        z.Error("sendMessage: msg is null");
                    }
                }else{
                    z.Error("sendMessage: session.isOpen() return false");
                }
            }else{
                z.Error("sendMessage: session is null");
            }
        } catch (IOException e) {
            z.Error("sendMessage: "+z.exceptionToString(e));
        }
    }



    /**
     * 验证message
     * @param message
     * @return
     */
    public Result vMessage(TextMessage message) {
        Result r = new Result();
        String text_message_string = message.getPayload();
        if(z.isNotNull(text_message_string)){
            try {
                Message m = JSONObject.parseObject(text_message_string, Message.class);
                if(z.isNotNull(m.getType())){
                    //判读Message.type是否合法
                    boolean type_is_ok = false;
                    for (MessageType mt: MessageType.values()){
                        if(mt.equals(m.getType())){
                            type_is_ok = true;
                            break;
                        }
                    }
                    if(type_is_ok){
                        //验证通过
                        r.setSuccess(m);
                    }else{
                        r.setError("消息JSON结构异常|Message.type值为属于非法值|"+text_message_string);
                    }
                }else{
                    r.setError("消息JSON结构异常|Message.type不可为空|"+text_message_string);
                }
            } catch (Exception e) {
                r.setError("消息JSON结构异常|"+text_message_string);
            }
        }else{
            r.setError("消息为空");
        }
        return r;
    }

}
